package com.lxu.service;

import com.lxu.entity.ReportDirEntity;
import com.lxu.entity.WordEntity;
import com.lxu.entity.bo.ReportTableBO;
import com.lxu.util.ReportToolsConstant;
import com.lxu.util.XWPFUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 模板数据填充接口
 */
@Service("wew14")
public class WordTemplateFillService {

    public void fill(ReportDirEntity dir, Map<String, Object> templateTextMap, List<ReportTableBO> templateTables) throws Exception {
        /**
         * 3.1、填充占位符
         */
        InputStream in = new FileInputStream(new File(dir.getTemplateFilePath()));
        WordEntity wordEntity = new WordEntity();
        wordEntity.setTextMap(templateTextMap);
        fillPlaceholder(wordEntity, in, dir.getDestFilePath());
        /**
         * 3.2、扩展表格行数
         */
        for (ReportTableBO templateTable : templateTables) {
            InputStream in2 = new FileInputStream(new File(dir.getDestFilePath()));
            WordEntity wordEntity2 = new WordEntity();
            wordEntity2.setTableMap(templateTable.getTemplateTables());
            wordEntity2.setStartRow(templateTable.getExportConfigEntity().getStartPosition());
            for (Map.Entry entry : templateTable.getTemplateTables().entrySet()) {
                wordEntity2.setRows(((List<List<String>>) entry.getValue()).size());
                String key = (String) entry.getKey();
                wordEntity2.setTableIndex(Integer.parseInt(key.substring(key.indexOf(ReportToolsConstant.TABLE_PREFIX) + 5, key.length())));
            }
            extendTableRows(wordEntity2, in2, dir.getDestFilePath());
        }
        /**
         * 3.3、填充表格扩展行数据
         */
        for (ReportTableBO templateTable : templateTables) {
            InputStream in3 = new FileInputStream(new File(dir.getDestFilePath()));
            WordEntity wordEntity3 = new WordEntity();
            wordEntity3.setTableMap(templateTable.getTemplateTables());
            for (Map.Entry entry : templateTable.getTemplateTables().entrySet()) {
                wordEntity3.setRows(((List<List<String>>) entry.getValue()).size());
            }
            wordEntity3.setStartRow(templateTable.getExportConfigEntity().getStartPosition());
            fillTableData(wordEntity3, in3, dir.getDestFilePath());
        }
    }

    /**
     * 填充占位符
     *
     * @param wordEntity
     * @throws Exception
     */
    public void fillPlaceholder(WordEntity wordEntity, InputStream in, String fileName) throws Exception {
        XWPFDocument xwpfDocument = XWPFUtils.fillComplexWord(in, wordEntity);
        XWPFUtils.save(fileName, xwpfDocument);
    }

    /**
     * @param wordEntity
     * @param in
     * @throws Exception
     */
    public void extendTableRows(WordEntity wordEntity, InputStream in, String fileName) throws Exception {
        XWPFDocument xwpfDocument = new XWPFDocument(in);
        XWPFTable table = XWPFUtils.getTableByIndex(xwpfDocument, wordEntity.getTableIndex());
        XWPFUtils.insertRows(table, wordEntity.getStartRow(), wordEntity.getRows() - ReportToolsConstant.INIT_TABLE_ROWS);
        XWPFUtils.save(fileName, xwpfDocument);
    }

    /**
     * 填充表格数据
     *
     * @param wordEntity
     * @param in
     * @throws Exception
     */
    public void fillTableData(WordEntity wordEntity, InputStream in, String fileName) throws Exception {
        XWPFDocument xwpfDocument = XWPFUtils.fillComplexWord(in, wordEntity);
        XWPFUtils.save(fileName, xwpfDocument);
    }
}